package com.thx.org.service;

import java.text.ParseException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thx.common.datasource.DataSource;
import com.thx.common.service.BaseManager;
import com.thx.org.dao.DepartmentDao;
import com.thx.org.model.Department;

@Service
@DataSource("dataSourceB")
@Transactional(readOnly = true)
public class DepartmentManager extends BaseManager<Department> {

	@Autowired
	private DepartmentDao deptDao;

	private OrgManagerImpl orgMgr;

	/**
	 * 获得企业的所有部门
	 * 部门以平级形式返回
	 * @param comId
	 *            企业ID
	 * @return
	 */
	public List<Department> findAllByComId(String comId) {
		return deptDao.findAllByComId(comId);
	}

	public List<Department> getByParentId(String depId) {
		return deptDao.getByParentId(depId);
	}

	public int getChildDeptNumByParentId(String depId) {
		return deptDao.getChildDeptNumByParentId(depId);
	}
	
	public List<Department> getAllDeps() {
		return deptDao.getAllDeps();

	}

	public Department findByName(String depName, String pid) {
		return deptDao.findByName(depName, pid);
	}
	
	public Department findByName(String depName) {
		return deptDao.findByName(depName);
	}

	public List<Department> getByParentId(String depId, int orderSeq) {
		return deptDao.getByParentId(depId, orderSeq);
	}

	@Transactional(readOnly = false)
	public boolean exSave(Department dep) {
		boolean ok = true;
		if (orgMgr != null)
			ok = ok && orgMgr.notify(orgMgr.EVENT_SAVE, dep);
		if (ok)
			deptDao.save(dep);

		return ok;
	}

	@Transactional(readOnly = false)
	public boolean exUpdate(Department dep) {
		boolean ok = true;
		if (orgMgr != null)
			ok = ok && orgMgr.notify(orgMgr.EVENT_UPDATE, dep);
		if (ok)
			deptDao.update(dep);

		return ok;
	}

	@Transactional(readOnly = false)
	public boolean exDelete(Department dep) {
		boolean ok = true;
		if (orgMgr != null)
			ok = ok && orgMgr.notify(orgMgr.EVENT_DELETE, dep);
		if (ok)
			deptDao.delete(dep);
		return ok;
	}

	@Transactional(readOnly = false)
	public boolean exDelete(String id) {

		boolean ok = true;
		if (orgMgr != null) {
			Department dep = new Department();
			dep.setDepId(id);
			ok = ok && orgMgr.notify(orgMgr.EVENT_DELETE, dep);
		}
		if (ok)
			deptDao.delete(id);
		return ok;
	}

	/**
	 * 假删
	 * 
	 * @param id
	 * @return
	 */
	@Transactional(readOnly = false)
	public boolean irExDelete(String id) {
		boolean ok = true;
		if (orgMgr != null) {
			Department dep = new Department();
			dep.setDepId(id);
			ok = ok && orgMgr.notify(orgMgr.EVENT_DELETE, dep);
		}

		if (ok) {
			Department dep = deptDao.get(id);
			dep.setDeleted("1");
			deptDao.update(dep);
		}
		return ok;
	}

	/**
	 * 递归删除
	 * 
	 * @param id
	 * @return
	 */
	@Transactional(readOnly = false)
	public boolean recursiveDelete(String id, boolean realDel) {
		boolean ok = true;

		List<Department> children = getByParentId(id);

		if (children != null && children.size() > 0) {
			for (Department dep : children) {
				ok = ok && recursiveDelete(dep.getDepId(), realDel);
				if (!ok)
					break;
			}
		}
		if (ok) {
			if (realDel)
				ok = ok && this.exDelete(id);
			else
				ok = ok && this.irExDelete(id);
		}
		return ok;
	}
	public List<Department> getAllByLastUpdateTime(String laseUpdateTime) throws ParseException {
		return deptDao.getAllByLastUpdateTime(laseUpdateTime);
	}
	
	public void setOrgMgr(OrgManagerImpl orgMgr) {
		this.orgMgr = orgMgr;
	}

	public Department findByFullName(String fullName) {
		return deptDao.findByFullName(fullName);
	}
	public  List<Department> getAllByParentId(String parentId){
		return deptDao.getAllByParentId(parentId);
	}
	
	/**
	 * @description 根据部门全路径查询该部门
	 * @author wjj
	 * @date 2014年12月18日
	 * Department
	 * @param depFunllName
	 * @return
	 */
	public Department getDepByDepFullName(String depFunllName){
		return deptDao.getDepByDepFullName(depFunllName);
	}
}
